package com.pet.platform.controller;

import com.pet.platform.common.PageResult;
import com.pet.platform.common.Result;
import com.pet.platform.dto.CommentDTO;
import com.pet.platform.dto.CommentQueryDTO;
import com.pet.platform.entity.Comment;
import com.pet.platform.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @GetMapping("/{id}")
    public Result<Comment> findById(@PathVariable Long id) {
        return Result.success(commentService.findById(id));
    }

    @GetMapping
    public Result<PageResult<CommentDTO>> findPage(CommentQueryDTO queryDTO) {
        return Result.success(commentService.findPage(queryDTO));
    }

    @GetMapping("/post/{postId}")
    public Result<List<CommentDTO>> findByPostId(@PathVariable Long postId) {
        return Result.success(commentService.findByPostId(postId));
    }

    @PostMapping
    public Result<Void> create(@Valid @RequestBody CommentDTO commentDTO) {
        commentService.create(commentDTO);
        return Result.success();
    }

    @PutMapping("/{id}")
    public Result<Void> update(@PathVariable Long id, @Valid @RequestBody CommentDTO commentDTO) {
        commentService.update(id, commentDTO);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        commentService.delete(id);
        return Result.success();
    }
} 